package com.yc.mybatis.mapper;

import com.yc.mybatis.entity.Goods;
import com.yc.mybatis.entity.GoodsEx;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface GoodsMapper {
    @Select("<script>" +
            "  select * from goods\n" +
            "        <where>\n" +
            "            <if test=\"id!=null\">\n" +
            "                and id=#{id}\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"name!=null\">\n" +
            "                and name=#{name}\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"nums!=null\">\n" +
            "                and nums>=#{nums}\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"bigNums!=null\">\n" +
            "                and nums &lt;=#{bigNums}\n" +
            "            </if>\n" +
            "        </where>" +
            "</script>")
    List<Goods> select(GoodsEx good);

    @Update("<script>" +
            "   update goods\n" +
            "        <set>\n" +
            "            <if test=\"name!=null\">\n" +
            "                 name=#{name},\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"type!=null\">\n" +
            "                type=#{type},\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"nums!=null\">\n" +
            "                nums=#{nums},\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"price!=null\">\n" +
            "                price=#{price},\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"state!=null\">\n" +
            "                state=#{state},\n" +
            "            </if>\n" +
            "        </set>\n" +
            "        where id=#{id}" +
            "</script>")
    void update(Goods good);

    @Select("select * from goods where id=#{id}")
    Goods selectById(Integer id);

    @Update("update goods set name=#{name},type=#{type}," +
            "nums=#{nums},price=#{price},state=#{state},")
    void updateAll(Goods good);

    List<Goods> selectByName(@Param("name") String name,
                             @Param("op") int op);

    List<Goods> selectByIds(@Param("ids") List<Integer> ids);

    List<Goods> selectByType(@Param("type") String type,
                             @Param("role") String role,
                             @Param("buy") Boolean buy,
                             @Param("sort") String sort);

    List<Map<String, Object>> selectsql(@Param("sql") String sql);
}
